var BASE_URL = "http://app.iqilu.com/v1";
var orgid = "275";
var pageSize = 10;
try {
  if (isPicture) {
    pageSize = 12;
  }
} catch (error) {
  console.log(error);
}
var totalNum = 50;
var newsList = [];
var _infos = "";
// 获取文稿列表  首次调取接口
$(function () {
  var allParams = window.location.href.split("?")[1] || "";
  var paramsList = allParams.split("&");
  var paramsObj = {};
  for (var i = 0; i < paramsList.length; i++) {
    var key = paramsList[i].split("=")[0] || "";
    var value = paramsList[i].split("=")[1] || "";
    paramsObj[key] = value;
  }
  if (!paramsObj._page) return;

  // 产出php的分页
  $(".paging").remove();

  page = paramsObj._page;
  $.ajax({
    url:
      BASE_URL +
      "/app/article/common/json?page=" +
      page +
      "&pagesize=" +
      pageSize +
      "&cateid=" +
      +cateid +
      "&_orgid_=" +
      orgid,
    type: "GET",
    dataType: "jsonp",
    jsonp: "cqcallback",
    success: function (res) {
      console.log(res, "rrr");
      newsList = res.data.infos;
      totalNum = res.data.total;
      $(".pagination").pagination({
        pageCount: totalNum > 0 ? Math.ceil(totalNum / pageSize) : 0,
        jump: false,
        current: page,
        coping: true,
        count: 3,
        homePage: "首页",
        endPage: "末页",
        prevContent: "上页",
        nextContent: "下页",
        callback: function (api) {
          page = api.getCurrent();
          handlePageChange(page);
        },
      });
      renderHtml();
    },
  });
});

function handlePageChange(page) {
  page = page || 1;
  $.ajax({
    url:
      BASE_URL +
      "/app/article/common/json?page=" +
      page +
      "&pagesize=" +
      pageSize +
      "&cateid=" +
      cateid +
      "&_orgid_=" +
      orgid,
    type: "GET",
    dataType: "jsonp",
    jsonp: "cqcallback",
    success: function (res) {
      if (+res.code !== 1) return;
      newsList = res.data.infos;
      renderHtml();
    },
  });
}

function renderHtml() {
  if (!newsList || newsList.length === 0) {
    newsList = [];
    return;
  }

  var htmlStr = template("getNews", { message: newsList });
  $("#news_list").html(htmlStr);
}
